package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.zycommunity.config.AutoLog;
import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysPost;
import com.blb.zycommunity.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 岗位信息表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
public class SysPostController {

    @Autowired
    private ISysPostService postService;

    @GetMapping("/posts")
    public ResponseResult<List<SysPost>> findAllPosts(){
        List<SysPost> allPosts = postService.findAllPosts();
        return ResponseResult.ok(allPosts);
    }

    @GetMapping("/post-pages")
    public ResponseResult<IPage<SysPost>> findPostPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                       @RequestParam(required = false,defaultValue = "5") Long size,
                                                       @RequestParam(required = false) String postCode,
                                                       @RequestParam(required = false) String postName,
                                                       @RequestParam(required = false) String status){
        IPage<SysPost> postPage = postService.findPostPage(current, size, postCode, postName, status);
        return ResponseResult.ok(postPage);
    }

    @PostMapping("/post")
    @AutoLog(value = 1,title = "部门")
    public ResponseResult savePost(@RequestBody SysPost sysPost){
        sysPost.setCreateTime(LocalDateTime.now());
        return ResponseResult.ok(postService.save(sysPost));
    }

    @PutMapping("/post")
    public ResponseResult updatePost(@RequestBody SysPost sysPost){
        sysPost.setUpdateTime(LocalDateTime.now());
        return ResponseResult.ok(postService.updateById(sysPost));
    }

    @DeleteMapping("/post/{postId}")
    public ResponseResult removePost(@PathVariable Long postId){
        return ResponseResult.ok(postService.removeById(postId));
    }

    @DeleteMapping("/posts/{postIds}")
    public ResponseResult removePosts(@PathVariable List<Long> postIds){
        return ResponseResult.ok(postService.removeBatchByIds(postIds));
    }

}
